package com.engine4cloud.tdf.education.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.engine4cloud.tdf.common.data.web.query.Queries;
import com.engine4cloud.tdf.common.security.service.TdfUser;
import com.engine4cloud.tdf.common.security.util.SecurityUtils;
import com.engine4cloud.tdf.education.convert.EduCourseGroupConvert;
import com.engine4cloud.tdf.education.entity.EduCourseGroupEntity;
import com.engine4cloud.tdf.education.mapper.EduCourseGroupMapper;
import com.engine4cloud.tdf.education.service.EduCourseGroupService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 课程学组关系
 *
 * @author wenjie
 * @date 2024-07-30 14:57:27
 */
@Service
public class EduCourseGroupServiceImpl extends ServiceImpl<EduCourseGroupMapper, EduCourseGroupEntity> implements EduCourseGroupService {
    @Override
    public List<EduCourseGroupEntity> findList(String courseCode) {
        LambdaQueryWrapper<EduCourseGroupEntity> wrapper = Queries.lambda();
        Queries.accept(courseCode, v -> wrapper.eq(EduCourseGroupEntity::getCourseCode, v));
        return super.list(wrapper);
    }

    @Override
    public void batchCreate(String courseCode, List<String> groupList) {
        super.saveBatch(EduCourseGroupConvert.INSTANCE.createList(courseCode, groupList, SecurityUtils.getUser().getTenantId()));
    }

    @Override
    public void batchUpdate(String courseCode, List<String> groupList) {
        TdfUser user = SecurityUtils.getUser();
        List<EduCourseGroupEntity> existList = this.findList(courseCode);

        List<EduCourseGroupEntity> preSaveList = this.preSaveList(courseCode, groupList, existList, user);
        if (CollectionUtils.isNotEmpty(preSaveList)) {
            super.saveBatch(preSaveList);
        }

        List<Long> preDeleteList = this.preDeleteList(groupList, existList);
        if (CollectionUtils.isNotEmpty(preDeleteList)) {
            super.removeBatchByIds(preDeleteList);
        }

    }

    private List<EduCourseGroupEntity> preSaveList(String courseCode, List<String> groupList, List<EduCourseGroupEntity> existList, TdfUser user) {
        if (CollectionUtils.isEmpty(groupList)) {
            return Collections.emptyList();
        }

        Map<String, EduCourseGroupEntity> existMap = existList.stream().collect(Collectors.toMap(EduCourseGroupEntity::getGroupCode, v -> v));
        List<EduCourseGroupEntity> preSaveList = new ArrayList<>();

        groupList.forEach(g -> {
            if (!existMap.containsKey(g)) {
                EduCourseGroupEntity entity = EduCourseGroupConvert.INSTANCE.create(courseCode, g, user.getTenantId());
                preSaveList.add(entity);
            }
        });

        return preSaveList;
    }

    private List<Long> preDeleteList(List<String> groupList, List<EduCourseGroupEntity> existList) {
        if (CollectionUtils.isEmpty(existList)) {
            return Collections.emptyList();
        }

        Map<String, EduCourseGroupEntity> existMap = existList.stream().collect(Collectors.toMap(EduCourseGroupEntity::getGroupCode, v -> v));
        List<Long> preDeleteList = new ArrayList<>();

        groupList.forEach(g -> {
            if (!existMap.containsKey(g)) {
                preDeleteList.add(existMap.get(g).getId());
            }
        });

        return preDeleteList;
    }

    private LambdaQueryWrapper<EduCourseGroupEntity> wrapper(String courseCode) {
        LambdaQueryWrapper<EduCourseGroupEntity> wrapper = Queries.lambda();
        Queries.accept(courseCode, v -> wrapper.eq(EduCourseGroupEntity::getCourseCode, v));
        return wrapper;
    }
}